package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.DoctorDto;
import primetest.dto.TreatmentRecordDto;
import primetest.faces.TreatmentRecordListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.TreatmentRecordService;

@Named("giveMedicineControler")
@SessionScoped
public class GiveMedicineControler implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 5248123092468632501L;
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Inject
	private SessionManager sessionManager;

	private TreatmentRecordListModel treatmentRecordList;
	private List<TreatmentRecordDto> baseTreatmentRecordList;
	private TreatmentRecordDto selectedTreatmentRecord;
	private int treatmentRecordListFirst;
	private List<TreatmentRecordDto> filteredTreatmentRecord;

	private DoctorDto doctor;
	@Inject
	private TreatmentRecordService treatmentRecordService;

	private boolean viewMode;
	private boolean giveMode;

	public TreatmentRecordListModel getTreatmentRecordList() {
		if (baseTreatmentRecordList == null) {
			try {
				baseTreatmentRecordList = new ArrayList<TreatmentRecordDto>();
				for (TreatmentRecordDto d : treatmentRecordService
						.getTreatmentRecordList()) {
					if (d.getStatus() == TreatmentRecordDto.FINISH_STATUS
							|| d.getStatus() == TreatmentRecordDto.PAID_STATUS) {
						baseTreatmentRecordList.add(d);
					}
				}
				treatmentRecordList = new TreatmentRecordListModel(
						baseTreatmentRecordList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseTreatmentRecordList == null ? new TreatmentRecordListModel(
				new ArrayList<TreatmentRecordDto>()) : treatmentRecordList;
	}

	public TreatmentRecordDto getSelectedTreatmentRecord() {
		return selectedTreatmentRecord;
	}

	public void setSelectedTreatmentRecord(
			TreatmentRecordDto selectedTreatmentRecord) {
		this.selectedTreatmentRecord = selectedTreatmentRecord;
	}

	public int getTreatmentRecordListFirst() {
		return treatmentRecordListFirst;
	}

	public void setTreatmentRecordListFirst(int first) {
		this.treatmentRecordListFirst = first;
	}
	public void giveMedicine() {
		try {
			selectedTreatmentRecord.setStatus(TreatmentRecordDto.FINISH_STATUS);
			selectedTreatmentRecord.setGiveMedicineDoctorDto(getDoctor());
			treatmentRecordService
					.updateTreatmentRecord(selectedTreatmentRecord);
			logger.debug("save the selectedTreatmentRecord" + " sympton: "
					+ selectedTreatmentRecord.getSymptom() + " seq: "
					+ selectedTreatmentRecord.getSeq());
			giveMode = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void startGiveMedicine() {
		giveMode = true;
	}

	public void cancelGiveMedicine() {
		giveMode = false;
	}

	public void viewMedicine() {
		viewMode = true;
	}

	public void endViewMeidicine() {
		viewMode = false;
	}

	public List<TreatmentRecordDto> getFilteredTreatmentRecord() {
		return filteredTreatmentRecord;
	}

	public void setFilteredTreatmentRecord(
			List<TreatmentRecordDto> filteredTreatmentRecord) {
		this.filteredTreatmentRecord = filteredTreatmentRecord;
	}

	public DoctorDto getDoctor() throws IOException {
		if (doctor == null) {
			doctor = sessionManager.getLoginUser();
		}
		return doctor;
	}

	public boolean isViewMode() {
		return viewMode;
	}

	public boolean isGiveMode() {
		return giveMode;
	}

	public boolean isViewOrGiveMode() {
		return viewMode || giveMode;
	}
}
